package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.commo.utils.R;
import com.qf.entity.Permission;
import com.qf.entity.Role;
import com.qf.entity.User;
import com.qf.service.IAuthService;
import com.qf.service.IPermissionService;
import com.qf.service.IRoleService;
import com.qf.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/auth")
public class AuthController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IAuthService authService;
    @Autowired
    private IPermissionService permissionService;

    @GetMapping("/unAuthRoleToUser/{uid}/{rid}")
    public R unAuthRoleToUser(@PathVariable Integer uid,@PathVariable Integer rid){
        Boolean flag=authService.unAuthRoleToUser(uid,rid);
        return R.out(flag);
    }
       @GetMapping("/getUserByRoleId/{roleId}")
    public R getUserByRoleId(@PathVariable Integer roleId){
           System.out.println(roleId);
           List<User> userList=userService.getUserByRoleId(roleId);

           return R.putDate("data",userList);
       }
       @PostMapping("/authRoleToUser/{roleId}")
    public R authRoleToUser(@PathVariable Integer roleId,@RequestBody List<User> list){
        authService.authRoleToUser(roleId,list);
           System.out.println("授权用户");
        return  R.ok();
       }
       @PostMapping("/authMenuToRole/{roleId}")
       public R authMenuToRole(@PathVariable Integer roleId){
           List<Permission> menuList = permissionService.treeList();
           List<Integer> roleMenuList = authService.getRoleMenuList(roleId);

           R r =R.ok();
           r.put("menuList",menuList);
           r.put("roleMenuList",roleMenuList);
           return r;
       }
    @PostMapping("/authMenuToroleUpdate/{roleId}")
    //问题未解决,这个
    public R authMenuToroleUpdate(@PathVariable Integer roleId,@RequestBody List<Integer> menuList){
        System.out.println(menuList.toString());
        return R.out(authService.authMenuToroleUpdate(roleId,menuList));
    }

}
